package edu.thu.studio.ormdesigner.outline.editpart;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.DragTracker;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.SelectEditPartTracker;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.ui.PlatformUI;

import edu.thu.studio.ormdesigner.Activator;
import edu.thu.studio.ormdesigner.model.AbstractModel;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.diagram.node.TableNodeSet;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNode;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNodeSet;
import edu.thu.studio.ormdesigner.outline.command.UpdateDiagramCommand;
import edu.thu.studio.ormdesigner.outline.dialog.DiagramDialog;
import edu.thu.studio.ormdesigner.util.Properties;

public class DiagramOutlineEditPart extends AbstractOutlineEditPart {
    
    @Override
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected List getModelChildren() {
        List children = new ArrayList<AbstractModel>();
        Diagram diagram = (Diagram) this.getModel();
        TableNodeSet tableNodes = new TableNodeSet();
        children.add(tableNodes);
        
        ViewNodeSet viewNodes = new ViewNodeSet();
        children.add(viewNodes);
        
        if (null != diagram.getNodes()) {
            for (Node node : diagram.getNodes()) {
                if (node instanceof ViewNode) {
                    viewNodes.addObject(node);
                } else {
                    tableNodes.addObject(node);
                }
            }
        }
        return children;
    }
    
    private int getTableViewNum() {
        int num = 0;
        Diagram diagram = (Diagram) this.getModel();
        if (null != diagram.getNodes()) {
            num += diagram.getNodes().getObjectList().size();
        }
        return num;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    protected void refreshOutlineVisuals() {
        Diagram model = (Diagram) this.getModel();
        
        this.setWidgetText(model.getName() + " (" + getTableViewNum() + ")");
        this.setWidgetImage(Activator.getImage(Properties.icon_diagram));
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public DragTracker getDragTracker(Request req) {
        return new SelectEditPartTracker(this);
    }
    
    public boolean isDeleteable() {
        return true;
    }
    
    @Override
    public void performRequest(Request request) {
        if (RequestConstants.REQ_OPEN.equals(request.getType())) {
            Diagram diagram = (Diagram) this.getModel();
            DiagramDialog dialog = new DiagramDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
                diagram);
            if (dialog.open() == IDialogConstants.OK_ID) {
                UpdateDiagramCommand command = new UpdateDiagramCommand(this.getORMModel(), diagram, dialog.getResult());
                this.execute(command);
            }
        }
    }
}
